﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ooganizer.API;
using Ooganizer.Server;
using Infragistics.Windows.DataPresenter;

namespace Ooganizer.Client.UI.WorkspaceSelectorLib
{
    /// <summary>
    /// Interaction logic for WorkspaceGrid.xaml
    /// </summary>
    public partial class WorkspaceGrid : Page
    {
        public WorkspaceGrid()
        {
            InitializeComponent();
        }
       

        private void OnWorkspaceSelectionChanged(object sender, RoutedEventArgs e)
        {
           //CellValuePresenter valuePresenter = (CellValuePresenter)sender;
        }

        private void OnWorkspaceSelectionDoubleClicked(object sender, MouseButtonEventArgs e)
        {            
            CellValuePresenter valuePresenter = (CellValuePresenter)sender;
            string selectedWorkspaceName = valuePresenter.Value.ToString();

            using (OoganizerState oogyState = new OoganizerState())
            {
                string currWorkspaceName = oogyState.GetCurrentWorkspace();

                using (OoganizerAPIProxy proxy = oogyState.GetAPIProxy())
                {                    
                    if (currWorkspaceName != null)
                    {
                        Workspace currentWorkspace = proxy.API.GetWorkspace(currWorkspaceName);

                        if (currentWorkspace.Unload())
                        {
                            oogyState.SetWorkspaceState(currWorkspaceName, OoganizerState.WorkspaceState.Unloaded);
                            oogyState.SetCurrentWorkspace(null);
                        }
                        else
                        {
                            // TODO: Log something here
                        }
                    }

                    oogyState.SetCurrentWorkspace(selectedWorkspaceName);
                    Workspace selectedWorkspace = proxy.API.GetWorkspace(selectedWorkspaceName);

                    if (selectedWorkspace != null)
                    {
                        if (selectedWorkspace.Load())
                        {
                            oogyState.SetCurrentWorkspace(selectedWorkspaceName);
                            oogyState.SetWorkspaceState(selectedWorkspaceName, OoganizerState.WorkspaceState.Shown);
                        }
                        else
                        {
                            //TODO: Log something here
                        }
                    }
                    else
                    {
                        //TODO: Log something here
                    }
                }
            }            
        }

        private void DataPresenter1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
